<?php session_start(); require_once('../connections/con_gl.php'); ?>
<?php
$act = $_GET[act];
$url = "index.php?component=sretail";
echo $act;
mysql_select_db($database_con_gl, $con_gl);
$query_gudang = "SELECT * FROM gl_gudang where id='$_SESSION[lokasi]'";
$gudang = mysql_query($query_gudang, $con_gl) or die(mysql_error());
$row_gudang = mysql_fetch_assoc($gudang);

// -- validasi
if(($act =='add') || ($act == 'edit')) { 
	if (trim($_POST['noreferensi']) == '') {
		$error[] = '- No Referensi harus diisi !!!';
	}
	if (trim($_POST['tanggal']) == '') {
		$error[] = '- Tanggal harus diisi !!!';
	}
	if (trim($_POST['dari']) == '') {
		$error[] = '- Customer harus dipilih !!!';
	}
	if (trim($_POST['barang']) == '') {
		$error[] = '- Paket Sewa harus dipilih !!!';
	}
	if (trim($_POST['kendaraan']) == '') {
		$error[] = '- Kendaraan harus dipilih !!!';
	}
	
	/*
	if (trim($_POST['transaksi']) == '') {
		$error[] = '- Detail Transaksi harus diisi !!!';
	}
	*/
	if ($_POST['ntotal'] <= 0) {
		$error[] = '- Periksa Total Transaksi anda !!!';
	}
}
else if ($act == 'bayar')
{
if (trim($_POST['keterangan']) == '') {
		$error[] = '- Keterangan harus diisi !!!';
	}
	if(intval($_POST['pembayaran'] > intval($_POST['kekurangan'])))
	{
		$error[] = '- Pembayaran melebihi nominal sisa pembayaran !!!';
	}

}
if (isset($error)) {
	echo "<img src=\"images/alert.png\" width=\"16\" align=\"left\"/>&nbsp;&nbsp;<b style=\"color:red;\">Error : </b> <br />".implode("<br />", $error);
}else{
	switch($act) {
	case("delete"):
		$query = "delete from gl_rtrans where no_ref='$_POST[noreferensi]'";
		$query2 = "delete from gl_trans where no_ref='$_POST[noreferensi]'";
		// hapus data
		mysql_select_db($database_con_gl, $con_gl);
		$runquery = mysql_query($query, $con_gl);
		if($runquery) { 
			// ---
			mysql_query($query2, $con_gl);
			// ---
			echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil dihapus ...";
			echo "<script type=\"text/javascript\">setTimeout(\"location.href='".$url."'\", 2000);</script>";
		}else{
			echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal dihapus !!!";
		}
	break;
	case("bayar"):
		$today = date("Y-m-d"); 
		$query = "INSERT INTO gl_pembayaran (no_ref, no_transaksi, tgl_bayar, jns_pemb, tipe_pemb, pmb_nominal, hold_status) VALUES 				                 ('$_POST[no_ref]','notrx','$today', '$_POST[cr_jenisbayar]','$_POST[cr_tipebayar]','$_POST[pembayaran]','$_SESSION[nama]' )";
		
		mysql_query("INSERT INTO gl_trans (id, kode, no_ref, transaksi, total, pos, akun) VALUES (NULL, 'KM','$_POST[no_ref]', 'Pembayaran', '$_POST[pembayaran]', 'D', '$_POST[cr_debit]')", $con_gl);
		
		mysql_query("INSERT INTO gl_trans (id, kode, no_ref, transaksi, total, pos, akun) VALUES (NULL, 'KM','$_POST[no_ref]', 'Pembayaran', '$_POST[pembayaran]', 'K', '$_POST[cr_kredit]')", $con_gl);
		
		// save data
		mysql_select_db($database_con_gl, $con_gl);
		$runquery = mysql_query($query, $con_gl) or die (mysql_error());
		if($runquery) { 
			echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil disimpan ...";

			echo "<script type=\"text/javascript\">window.opener.location.href='".$url."'; window.close();</script>";
		}else{
			echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal disimpan !!!";
		}
	break;
	case("edit"):
	// insert piutang
	if($_POST['a_piu']<>'' AND $_POST[ntotal] > 0) {
  			$nil = $_POST[ntotal];
  			mysql_select_db($database_con_gl, $con_gl);
  			if($_POST[a_piu]=='') {
  				mysql_query("INSERT INTO gl_trans (id, no_ref, transaksi, total, pos, akun) VALUES (NULL, '$_POST[noreferensi]', 'Piutang Penjualan $row_getkpd[nama]', '$_POST[ntotal]', 'D', '$_POST[a_piu]')", $con_gl);
  			}else{
				
  				mysql_query("update gl_trans set total='$nil', pos='D' where akun='$_POST[a_piu]' and no_ref='$_POST[noreferensi]' and kode='SJ'", $con_gl);
  			}
  		}else{
			if($_POST[a_piu]<>'') {
				mysql_query("delete from gl_trans where akun='$_POST[a_piu]' and no_ref='$_POST[noreferensi]'", $con_gl);
			}
		}
		
		// insert penjualan
	if($_POST['a_pend']<>'' AND $_POST[hargasatuan] > 0) {
  			
  			mysql_select_db($database_con_gl, $con_gl);
  			if($_POST[a_pend]=='') {
  				mysql_query("INSERT INTO gl_trans (id, kode,no_ref, transaksi, total, pos, akun) VALUES (NULL,'SJ', '$_POST[noreferensi]', 'Penjualan Rental Kendaraan Retail $row_getkpd[nama]', '$penj', 'K', '$_POST[a_pend]')", $con_gl) or die (mysql_error());
  			}else{
				
  				mysql_query("update gl_trans set total='$penj', pos='K' where akun='$_POST[a_pend]' and no_ref='$_POST[noreferensi]' and kode='SJ'", $con_gl);
  			}
  		}else{
			if($_POST[a_pend]<>'') {
				mysql_query("delete from gl_trans where akun='$_POST[a_pend]' and no_ref='$_POST[noreferensi]'", $con_gl);
			}
		}
		// insert PPH
	if($_POST['a_pph']<>'' AND $_POST[hargaafterpph] > 0) {
  			$query_cekpph = "SELECT akun from gl_trans where akun='$_POST[a_pph]' and no_ref ='$_POST[noreferensi]' and kode='SJ' and pos='D'";
$pph = mysql_query($query_cekpph, $con_gl) or die(mysql_error());
$cekpph = mysql_num_rows($pph);
			    			
  			if($cekpph > 0) {
				mysql_select_db($database_con_gl, $con_gl);
				echo 'update mang';
				mysql_query("update gl_trans set total='$_POST[hargaafterpph]', pos='D' where akun='$_POST[a_pph]' and no_ref='$_POST[noreferensi]' and kode='SJ'", $con_gl);
  			}else{
			    // karena pajak pph standar semua area jadi hardcode untuk delete 
				mysql_query("INSERT INTO gl_trans (id, kode,no_ref, transaksi, total, pos, akun) VALUES (NULL,'SJ', '$_POST[noreferensi]', 'Beban PPH 23', '$_POST[hargaafterpph]', 'D', '$_POST[a_pph]')", $con_gl) or die (mysql_error());
  			}
  		}else{
			mysql_query("delete from gl_trans where akun='215001' and no_ref='$_POST[noreferensi]' and kode='SJ'", $con_gl);
			
		}
		//
		// insert ppn
	if($_POST['a_paj']<>'' AND $_POST[hargaafterpph] > 0) {
  			$query_cekpph = "SELECT akun from gl_trans where akun='$_POST[a_paj]' and no_ref ='$_POST[noreferensi]' and kode='SJ' and pos='K'";
$pph = mysql_query($query_cekpph, $con_gl) or die(mysql_error());
$cekpph = mysql_num_rows($pph);
			    			
  			if($cekpph > 0) {
				mysql_select_db($database_con_gl, $con_gl);
				//echo 'update mang';
				mysql_query("update gl_trans set total='$_POST[hargaafterppn]', pos='K' where akun='$_POST[a_paj]' and no_ref='$_POST[noreferensi]' and kode='SJ'", $con_gl);
  			}else{
			    // karena pajak pph standar semua area jadi hardcode untuk delete 
				mysql_query("INSERT INTO gl_trans (id, kode,no_ref, transaksi, total, pos, akun) VALUES (NULL,'SJ', '$_POST[noreferensi]', 'Beban PPN', '$_POST[hargaafterppn]', 'K', '$_POST[a_paj]')", $con_gl) or die (mysql_error());
  			}
  		}else{
			mysql_query("delete from gl_trans where akun='117002' and no_ref='$_POST[noreferensi]' and kode='SJ'", $con_gl);
			
		}
				
	mysql_select_db($database_con_gl, $con_gl);
	$today = date("Y-m-d"); 
	if($_POST['jumlah'] == '0')
		{
			$jmlhari = '1';
		} else {$jmlhari = $_POST['jumlah'];}
		$query ="UPDATE gl_retailtrans SET tgl='$_POST[tanggal]', keterangan='$_POST[keterangan]', jenis='$_POST[jenis]', area='$_POST[area]', gudang='$_POST[gudang]', barang='$_POST[barang]', kontak='$_POST[dari]', recipient='$_POST[recipient]', jatuhtempo='$_POST[checkout]', tujuan='$_POST[tujuan]', pengemudi='$_POST[pengemudi]', jmlhr='$jmlhari', jmlwkt='$_POST[jmlhwkt]', checkin='$_POST[checkin]', waktucheckin='$_POST[waktucheckin]', checkout='$_POST[checkout]', waktucheckout='$_POST[waktucheckout]', supir='$_POST[supir]', bbm='$_POST[bbm]', tol='$_POST[tol]', parkir='$_POST[parkir]', akomod='$_POST[akomodasi]', other='$_POST[other]', kendaraan='$_POST[ken]',updated_by = '$_SESSION[nama]', updated_at = '$today' WHERE no_ref='$_POST[noreferensi]'";
		
	    mysql_select_db($database_con_gl, $con_gl);
		$total = ($_POST['hargasatuan'] + $_POST['hargaafterppn'] - $_POST['diskon']); 
		if(!empty($_POST['xharga'])) {
		    $hargasatuan = $_POST['xharga'];
		}
		else
		{
			$hargasatuan = $_POST['hargasatuansebelum'];
		}
		$addquery = "UPDATE gl_detailtrans SET transaksi='$_POST[transaksi]', total='$total', barang='$_POST[barang]', kendaraan='$_POST[txtkendaraan]', ppn='$_POST[hargaafterppn]',pph='$_POST[hargaafterpph]', hargasatuan='$hargasatuan', diskon='$_POST[diskon]',diskonamount='$_POST[txtdiskon]' where no_ref = '$_POST[noreferensi]'";
	
		mysql_query($addquery, $con_gl) or die (mysql_error());
		$runquery = mysql_query($query, $con_gl) or die (mysql_error());
		if($runquery) { 
			echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil disimpan ...";
			echo "<script type=\"text/javascript\">setTimeout(\"location.href='".$url."'\", 2000);</script>";
		}else{
			echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal disimpan !!!";
		}
	break;
	default:
		mysql_select_db($database_con_gl, $con_gl);		
		

		$query_getkpd = "SELECT gl_kontak.nama FROM gl_kontak WHERE gl_kontak.id='$_POST[dari]'";
		$getkpd = mysql_query($query_getkpd, $con_gl) or die(mysql_error());
		$row_getkpd = mysql_fetch_assoc($getkpd);
		// ---
		$today = date("Y-m-d"); 
		if($_POST['jumlah'] == '0')
		{
			$jmlhari = '1';
		} else {$jmlhari = $_POST['jumlah'];}
		$query = "INSERT INTO gl_retailtrans (no_ref, jnsorder,tgl, keterangan, area, gudang, barang, kontak, jenis, jatuhtempo, tujuan, recipient, pengemudi,  jmlhr, jmlwkt, checkin, waktucheckin, checkout, waktucheckout, supir, bbm, tol, parkir, akomod, other, kendaraan,created_by,created_at) VALUES ('$_POST[noreferensi]','0', '$_POST[tanggal]', '$_POST[keterangan]', '$_POST[area]', '$_POST[gudang]', '$_POST[barang]', '$_POST[dari]', '$_POST[jenis]', '$_POST[checkout]','$_POST[tujuan]', '$_POST[recipient]', '$_POST[pengemudi]','$jmlhari','$_POST[jmlhwkt]','$_POST[checkin]', '$_POST[waktucheckin]', '$_POST[checkout]', '$_POST[waktucheckout]', '$_POST[supir]', '$_POST[bbm]', '$_POST[tol]', '$_POST[parkir]', '$_POST[akomodasi]', '$_POST[other]','$_POST[kendaraan]','$_SESSION[nama]','$today')";
		// [ Jurnal Penjualan ]
		// insert piutang
		
		if($_POST['a_piu']<>'' AND $_POST['ntotal'] > 0) {
  			//$nil = $_POST['ntotal'];
  		$nil = (int)$_POST['hargasatuan'] + (int)$_POST['hargaafterppn'] + (int)$_POST['biaya'] - (int)$_POST['hargaafterpph'];
			mysql_select_db($database_con_gl, $con_gl); 
 			mysql_query("INSERT INTO gl_trans (id, kode,no_ref, transaksi, total, pos, akun) VALUES (NULL,'SJ', '$_POST[noreferensi]', 'Piutang Penjualan $row_getkpd[nama]', '$nil', 'D', '$_POST[a_piu]')", $con_gl) or die (mysql_error());
  		}
		// penjualan 
		
		if($_POST['a_pend']<>'' AND $_POST['hargasatuan'] > 0) {
  			$penj = $_POST['hargasatuan'] + (int)$_POST['txtdiskon'];
  			mysql_select_db($database_con_gl, $con_gl); 
 			mysql_query("INSERT INTO gl_trans (id, kode,no_ref, transaksi, total, pos, akun) VALUES (NULL,'SJ', '$_POST[noreferensi]', 'Penjualan Rental Kendaraan Retail $row_getkpd[nama]', '$penj', 'K', '$_POST[a_pend]')", $con_gl) or die (mysql_error());
  		}
		// Potongan
		if($_POST['a_potongan']<>'' AND $_POST['txtdiskon'] > 0) {
  			$potongan = $_POST['txtdiskon'];
  			mysql_select_db($database_con_gl, $con_gl); 
 			mysql_query("INSERT INTO gl_trans (id, kode,no_ref, transaksi, total, pos, akun) VALUES (NULL,'SJ', '$_POST[noreferensi]', 'Potongan Penjualan [Diskon] $row_getkpd[nama]', '$potongan', 'D', '$_POST[a_potongan]')", $con_gl) or die (mysql_error());
  		}
		// insert PPH
		if($_POST[a_pph] <>'' AND $_POST[hargaafterpph] > 0) {
  			mysql_select_db($database_con_gl, $con_gl);
  			mysql_query("INSERT INTO gl_trans (id, kode,no_ref, transaksi, total, pos, akun) VALUES (NULL,'SJ', '$_POST[noreferensi]', 'Beban PPH 23', '$_POST[hargaafterpph]', 'D', '$_POST[a_pph]')", $con_gl) or die (mysql_error());
 		}
			// insert pajak
  		if($_POST[a_paj] <>'' AND $_POST[hargaafterppn] > 0) {
  			mysql_select_db($database_con_gl, $con_gl); 
 			mysql_query("INSERT INTO gl_trans (id, kode,no_ref, transaksi, total, pos, akun) VALUES (NULL,'SJ', '$_POST[noreferensi]', 'Beban PPN', '$_POST[hargaafterppn]', 'K', '$_POST[a_paj]')", $con_gl) or die (mysql_error());
  		}
		// insert tol parkir
		$biaya = (int)$_POST[tol] + (int)$_POST[parkir];
		if($_POST[a_beb] <>'' AND $biaya > 0) {
			$biaya = (int)$_POST[tol] + (int)$_POST[parkir];
  			mysql_select_db($database_con_gl, $con_gl);
  			mysql_query("INSERT INTO gl_trans (id, kode,no_ref, transaksi, total, pos, akun) VALUES (NULL,'SJ', '$_POST[noreferensi]', 'Beban Biaya Lain [Tol Parkir]', '$biaya', 'K', '$_POST[a_beb]')", $con_gl) or die (mysql_error());
 		}
		// insert tol bbm
		if($_POST[a_bbm] <>'' AND $_POST[bbm] > 0) {
			
  			mysql_select_db($database_con_gl, $con_gl);
  			mysql_query("INSERT INTO gl_trans (id, kode,no_ref, transaksi, total, pos, akun) VALUES (NULL,'SJ', '$_POST[noreferensi]', 'Beban Biaya BBM', '$_POST[bbm]', 'K', '$_POST[a_bbm]')", $con_gl) or die (mysql_error());
 		}
		// insert akomodasi a_jasadriver
			if($_POST[a_jasadriver] <>'' AND $_POST[supir] > 0) {
			
  			mysql_select_db($database_con_gl, $con_gl);
  			mysql_query("INSERT INTO gl_trans (id, kode,no_ref, transaksi, total, pos, akun) VALUES (NULL,'SJ', '$_POST[noreferensi]', 'Beban Biaya Jasa Driver', '$_POST[supir]', 'K', '$_POST[a_jasadriver]')", $con_gl) or die (mysql_error());
 		}
	    // insert akomodasi / uang makan
		if($_POST[a_insentifdriver] <>'' AND $_POST[akomodasi] > 0) {
			
  			mysql_select_db($database_con_gl, $con_gl);
  			mysql_query("INSERT INTO gl_trans (id, kode,no_ref, transaksi, total, pos, akun) VALUES (NULL,'SJ', '$_POST[noreferensi]', 'Beban Biaya Uang Makan', '$_POST[akomodasi]', 'K', '$_POST[a_insentifdriver]')", $con_gl) or die (mysql_error());
 		}
		// Jurnal Biaya Operasional
		// Kas Kecil di kredit, pengurangan
		if($_POST[biaya] > 0)
		{
		if($_POST[a_kaskecil] <>'' AND $_POST[biaya]) {
			$totbiaya = (int)$_POST[biaya];
  			mysql_select_db($database_con_gl, $con_gl);
  			mysql_query("INSERT INTO gl_trans (id, kode,no_ref, transaksi, total, pos, akun) VALUES (NULL,'KK', '$_POST[noreferensi]', 'Kas Kecil', '$totbiaya', 'K', '$_POST[a_kaskecil]')", $con_gl) or die (mysql_error());
 		}
		// 
		$biaya = (int)$_POST[tol] + (int)$_POST[parkir];
		if($_POST[a_beb] <>'' AND $biaya > 0) {
			$biaya = (int)$_POST[tol] + (int)$_POST[parkir];
  			mysql_select_db($database_con_gl, $con_gl);
  			mysql_query("INSERT INTO gl_trans (id, kode,no_ref, transaksi, total, pos, akun) VALUES (NULL,'KK', '$_POST[noreferensi]', 'Beban Biaya Lain [Tol Parkir]', '$biaya', 'D', '$_POST[a_beb]')", $con_gl) or die (mysql_error());
 		}
		// insert tol bbm
		if($_POST[a_bbm] <>'' AND $_POST[bbm] > 0) {
			
  			mysql_select_db($database_con_gl, $con_gl);
  			mysql_query("INSERT INTO gl_trans (id, kode,no_ref, transaksi, total, pos, akun) VALUES (NULL,'KK', '$_POST[noreferensi]', 'Beban Biaya BBM', '$_POST[bbm]', 'D', '$_POST[a_bbm]')", $con_gl) or die (mysql_error());
 		}
		// insert akomodasi a_jasadriver
			if($_POST[a_jasadriver] <>'' AND $_POST[supir] > 0) {
			
  			mysql_select_db($database_con_gl, $con_gl);
  			mysql_query("INSERT INTO gl_trans (id, kode,no_ref, transaksi, total, pos, akun) VALUES (NULL,'KK', '$_POST[noreferensi]', 'Beban Biaya Jasa Driver', '$_POST[supir]', 'D', '$_POST[a_jasadriver]')", $con_gl) or die (mysql_error());
 		}
	    // insert akomodasi / uang makan
		if($_POST[a_insentifdriver] <>'' AND $_POST[akomodasi] > 0) {
			
  			mysql_select_db($database_con_gl, $con_gl);
  			mysql_query("INSERT INTO gl_trans (id, kode,no_ref, transaksi, total, pos, akun) VALUES (NULL,'KK', '$_POST[noreferensi]', 'Beban Biaya Uang Makan', '$_POST[akomodasi]', 'D', '$_POST[a_insentifdriver]')", $con_gl) or die (mysql_error());
 		}
		}
		else 
		{
			// tidak ada aktifitas
		}
		// tambah variable grandtotal	
  			
		mysql_select_db($database_con_gl, $con_gl);
		$addquery = "INSERT INTO gl_detailtrans (id, no_ref, transaksi, total, barang, kendaraan, ppn,pph, hargasatuan, diskon,diskonamount) VALUES (NULL, '$_POST[noreferensi]', '$_POST[transaksi]', '$_POST[hargasatuan]', '$_POST[barang]', '$_POST[kendaraan]', '$_POST[hargaafterppn]','$_POST[hargaafterpph]','$_POST[xharga]', '$_POST[diskon]','$_POST[txtdiskon]')";
		mysql_query($addquery, $con_gl) or die (mysql_error());
		// save data
		mysql_select_db($database_con_gl, $con_gl);
		$runquery = mysql_query($query, $con_gl) or die (mysql_error());
		if($runquery) { 
			echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil disimpan ...";
			echo "<script type=\"text/javascript\">setTimeout(\"location.href='".$url."'\", 2000);</script>";
		}else{
			echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal disimpan !!!";
		}
		
		
		
		
	break;
	}
}
?>